package problem6;

//6.z字形变换
//https://leetcode.cn/problems/zigzag-conversion/

class Solution {
    public String convert(String s, int numRows) {
        if(numRows == 1) return s;
        int n = s.length();
        StringBuffer ret = new StringBuffer("");
        int d = 2 * numRows - 2;
        for(int i = 0; i<numRows; i++) {
            if(i == 0 || i == numRows-1) {
                for(int j = i; j<n; j+=d) {
                    ret.append(s.charAt(j));
                }
            }else {
                for(int j = i, k = d-i; j<n; j+=d, k+=d) {
                    ret.append(s.charAt(j));
                    if(k < n) ret.append(s.charAt(k));
                }
            }
        }
        return ret.toString();
    }
}